package com.itheima.huikebackend.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.huikebackend.common.PageResult;
import com.itheima.huikebackend.context.BaseContext;
import com.itheima.huikebackend.dto.SysPostDTO;
import com.itheima.huikebackend.dto.SysPostPageDTO;
import com.itheima.huikebackend.entity.SysPost;
import com.itheima.huikebackend.mapper.SysPostMapper;
import com.itheima.huikebackend.service.SysPostService;
import com.itheima.huikebackend.vo.SysPostVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Slf4j
public class SysPostServiceImpl implements SysPostService {
    private final SysPostMapper sysPostMapper;

    /**
     * 岗位分页查询
     *
     * @param sysPostPageDTO sys帖子页面dto
     * @return {@link PageResult}
     */
    @Override
    public Map<String, Object> page(SysPostPageDTO sysPostPageDTO) {
        //开始分页
        PageHelper.startPage(sysPostPageDTO.getPageNum(), sysPostPageDTO.getPageSize());
        SysPost sysPost = new SysPost();
        BeanUtils.copyProperties(sysPostPageDTO, sysPost);
        Page<SysPost> sysPostPage = sysPostMapper.pageQuery(sysPost);

        Map<String, Object> map = new HashMap<>();
        map.put("total", sysPostPage.getTotal());
        map.put("rows", sysPostPage.getResult());
        return map;
    }

    /**
     * 根据id查询岗位
     *
     * @param id id
     * @return {@link SysPostVO}
     */
    @Override
    public SysPostVO getById(Long id) {
        SysPost sysPost = sysPostMapper.getById(id);
        SysPostVO sysPostVO = new SysPostVO();
        BeanUtils.copyProperties(sysPost, sysPostVO);
        return sysPostVO;
    }

    /**
     * 更新
     *
     * @param sysPostDTO sys dto后
     */
    @Override
    public void update(SysPostDTO sysPostDTO) {
        SysPost sysPost = new SysPost();

        sysPost.setPostCode(sysPostDTO.getPostCode());
        sysPost.setPostName(sysPostDTO.getPostName());
        sysPost.setPostSort(sysPostDTO.getPostSort());
        sysPost.setStatus(sysPostDTO.getStatus());
        sysPost.setCreateBy(BaseContext.getCurrentId());
        sysPost.setCreateTime(LocalDateTime.now());
        sysPost.setPostId(sysPostDTO.getPostId());
        sysPost.setRemark(sysPostDTO.getRemark());


        //更新修改人 修改时间
        sysPost.setUpdateBy(BaseContext.getCurrentId());
        sysPost.setUpdateTime(LocalDateTime.now());

        sysPostMapper.update(sysPost);
    }

    /**
     * 添加
     *
     * @param sysPostDTO sys dto后
     */
    @Override
    public void add(SysPostDTO sysPostDTO) {

        SysPost sysPost = new SysPost();
        BeanUtils.copyProperties(sysPostDTO, sysPost);
        sysPost.setCreateTime(LocalDateTime.now());
        sysPost.setUpdateTime(LocalDateTime.now());
        sysPost.setUpdateBy(BaseContext.getCurrentId());
        sysPost.setCreateBy(BaseContext.getCurrentId());
        sysPostMapper.insert(sysPost);


    }

    /**
     * 删除通过id
     *
     * @param id id
     */
    @Override
    public void deleteById(Long id) {
        sysPostMapper.deleteById(id);
    }

    /**
     * 表格
     *
     * @param response 响应
     */
    @Override
    public void export(HttpServletResponse response) {
        // 1.读取Excel
        //获取动态路径
        // 1.读取Excel
        //获取动态路径

        try {
            InputStream inp = this.getClass().getClassLoader().getResourceAsStream("post.xls");
            Workbook wb = WorkbookFactory.create(inp);
            // 2.修改Excel表格
            // 2.1拿到sheet页
            Sheet sheetAt0 = wb.getSheetAt(0);
            // 2.2生成时间
            LocalDate now = LocalDate.now();
            LocalDate begin = now.plusDays(-30);
            LocalDate end = now.plusDays(-1);
            String time = "生成时间:" + begin + "至" + end;
            Sheet sheet = wb.createSheet("岗位信息");
            // 创建标题行
            Row headerRow = sheet.createRow(0);
            List<String> headers = Arrays.asList("岗位ID", "岗位编码", "岗位名称", "岗位排序", "状态", "创建时间");
            for (int i = 0; i < headers.size(); i++) {
                Cell cell = headerRow.createCell(i);
                cell.setCellValue(headers.get(i));
            }
            int rowNum = 1;
            List<SysPostDTO> list = new ArrayList<>();

            for (SysPostDTO sysPostDTO : list) {
                Row row = sheet.createRow(rowNum++);
                row.createCell(0).setCellValue(sysPostDTO.getPostId());
                row.createCell(1).setCellValue(sysPostDTO.getPostCode());
                row.createCell(2).setCellValue(sysPostDTO.getPostName());
                row.createCell(3).setCellValue(sysPostDTO.getPostSort());
                row.createCell(4).setCellValue(sysPostDTO.getStatus());
                row.createCell(5).setCellValue(sysPostDTO.getCreateTime().toString());
            }


            ServletOutputStream outputStream = response.getOutputStream();
            wb.write(outputStream);

        } catch (Exception e) {
            log.error("表格导出失败:{}", e.getMessage());
        }
    }

    /**
     * 删除批处理
     *
     * @param ids id
     */
    @Override
    public void deleteBatch(List<Long> ids) {
//        String[] split = ids.split(",");
//        List<Integer> integers = Arrays.stream(split).map(Integer::parseInt).collect(Collectors.toList());
        sysPostMapper.deleteByIds(ids);
    }


}
